package com.boot.config;

import org.apache.kafka.streams.state.RocksDBConfigSetter;
import org.rocksdb.BlockBasedTableConfig;
import org.rocksdb.CompactionStyle;
import org.rocksdb.CompressionType;
import org.rocksdb.Options;

import java.util.Map;

/**
 * 自定义rocksdb的配置 用于调优
 */
public class CustomRocksDBConfigSetter implements RocksDBConfigSetter {
    @Override
    public void setConfig(String storeName, Options options, Map<String, Object> configs) {
        BlockBasedTableConfig config = new BlockBasedTableConfig();
        config.setBlockCacheSize(50 * 1024 * 1024); //50MB对于“大数据”来说明显太小。大小需要平衡
        config.setBlockSize(4 * 1024 * 1024);//太小读太慢，太大读放大。硬盘好建议调大
        options.setTableFormatConfig(config);
        options.setWalTtlSeconds(10);
        options.setWriteBufferSize(16 * 1024 * 1024);//16MB ,太小写放大，太大flush太慢，没有WAL时宕机会丢失数据

        options.setCompressionType(CompressionType.LZ4_COMPRESSION);
        options.setCompactionStyle(CompactionStyle.FIFO);

        options.setMaxWriteBufferNumber(4);
    }

    @Override
    public void close(String storeName, Options options) {

    }
}
